//
//  CZVisitorView.swift
//  WeiBo
//
//  Created by Apple on 16/9/5.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit


//  视图视图代理
protocol CZVisitorViewDelegate: NSObjectProtocol {
    //  登录操作的代理方法
    func loginOperation()
}

//  访客视图
class CZVisitorView: UIView {
    
    //  定义访客视图代理对象
    weak var delegate: CZVisitorViewDelegate?
    
    //  执行登录操作的闭包的回调
    var loginOperationCallBack: (() -> ())?
    // MARK:    -懒加载控件
    //  fileprivate表示私有的变量，private表示更加私有， 分类里面私有的变量也不能访问
    //  旋转视图
    fileprivate lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //  罩层视图
    fileprivate lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //  主页视图
    fileprivate lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //  消息
    fileprivate lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFont(ofSize: 13)
        label.textAlignment = .center
        //  多行显示
        label.numberOfLines = 0
        //  设置颜色
        label.textColor = UIColor.darkGray
        return label
    }()
    //  注册
    fileprivate lazy var registerButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(registerButtonAction), for: .touchUpInside)
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        button.adjustsImageWhenHighlighted = false
        return button
    }()
    //  登录
    fileprivate lazy var loginButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(loginButtonAction), for: .touchUpInside)
        button.setTitle("登录", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        button.adjustsImageWhenHighlighted = false
        return button
    }()
    
    
    
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //  添加控件设置约束
    fileprivate func setupUI() {
        
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        //  先添加视图，然后在设置约束
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        //  使用系统的约束
//        cycleImageView.translatesAutoresizingMaskIntoConstraints = false
        
        //  设置中心点等于当前视图的中心点
//        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: NSLayoutAttribute.centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: NSLayoutAttribute.centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
        

        //  使用snapkit进行约束
        
        cycleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(cycleImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.width.equalTo(224)
        }
        
        loginButton.snp_makeConstraints { (make) in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        registerButton.snp_makeConstraints { (make) in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(loginButton)
            make.size.equalTo(loginButton)
        }
        
        
    }
    
    
    //  开启动画
    fileprivate func startAnimation() {
     
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //  设置目的地
        animation.toValue = 2 * M_PI
        //  动画时长
        animation.duration = 20
        //  执行次数
        animation.repeatCount = MAXFLOAT
        //  不让其是否动画
        animation.isRemovedOnCompletion = false
        
        //  执行动画
        cycleImageView.layer.add(animation, forKey: nil)
        
        
    }
    
    // MARK:    -点击事件
    @objc private func registerButtonAction() {
    
        print("注册")
        loginOperationCallBack?()
        //  通过代理对象执行代理方法
        delegate?.loginOperation()
    
    }
    
    @objc private func loginButtonAction() {
        print("登录")
        loginOperationCallBack?()
        delegate?.loginOperation()
    }
    
    //  更新访客视图信息
    func updateVisitorViewInfo(message: String?, imageName: String?) {
        //  条件成立是 消息、发现、我的
        if let msg = message, let imgName = imageName {
            messageLabel.text = msg
            iconImageView.image = UIImage(named: imgName)
            //  隐藏旋转视图
            cycleImageView.isHidden = true
        } else {
            //  显示旋转视图
            cycleImageView.isHidden = false
            //  开启旋转动画
            startAnimation()
        }
        
    }
    
    
}



